package br.org.credfaz.sis.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import br.org.credfaz.sis.MysqlConnectionFactory;
import br.org.credfaz.sis.modelo.Contratomutuo;

public class ContratomutuoDAO {
	
private final Connection connection;
	
	public ContratomutuoDAO(){
		try{
			connection = new MysqlConnectionFactory().getConnection();
		}catch (SQLException e){
			throw new RuntimeException(e);
		}
	}
	
	public void adiciona(Contratomutuo contratomutuo){
		String sql = "insert into contratomutuo (idcliente, numcontratomutuo, descinfadicional, qtdparcelaspag) values (?, ?, ?, ?)";
		PreparedStatement stmt;
		try{
			stmt = connection.prepareStatement(sql);
			
			stmt.setInt(1, contratomutuo.getIdcliente());
			stmt.setFloat(2, contratomutuo.getNumcontratomutuo());
			stmt.setString(3, contratomutuo.getDescinfadicional());
			stmt.setInt(4, contratomutuo.getQtdparcelaspag());
			
			
			stmt.execute();
			
		}catch (SQLException e){
			throw new RuntimeException(e);
		}
	}

	public void atualizaAvalista(Contratomutuo cmutuo) {
		String sql = "update contratomutuo SET possuiavalista = ? where numcontratomutuo = ?";
		
		PreparedStatement stmt;
		try{
			stmt = connection.prepareStatement(sql);
			
			stmt.setBoolean(1, cmutuo.getPossuiAvalista());
			stmt.setFloat(2, cmutuo.getNumcontratomutuo());
			
			stmt.execute();
			
		}catch (SQLException e){
			throw new RuntimeException(e);
		}
		
	}

}
